package com.gaojun;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javax.swing.filechooser.FileSystemView;

import com.gaojun.tool.Constant;
import com.gaojun.tool.ConvertVideo;
import com.gaojun.tool.Video;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * 转换视频格式
 * @author gj
 *
 */
public class ChangeVideoType extends Stage {

	private double xOffset = 0;
	private double yOffset = 0;
	private String changeType = ".mp4";
	private TableView<Video> table = new TableView<>();
	private ObservableList<Video> data = FXCollections.observableArrayList();
	private Video selectItem = null;

	public ChangeVideoType() {
		// TODO Auto-generated constructor stub
		String style = "-fx-background-color:#00000000";
		String styleEntered = "-fx-background-color:#802a2a";
		Pane pane = new Pane();
		pane.setPrefSize(800, 500);
		pane.setStyle("-fx-background-color:#292421");
		Button btnClose = new Button("");
		btnClose.setGraphic(new ImageView(new Image("res/image/stage/closestage.png")));
		btnClose.setLayoutX(760);
		btnClose.setLayoutY(2);
		btnClose.setStyle(style);
		btnClose.setOnMouseEntered(e -> {
			btnClose.setStyle(styleEntered);
		});

		btnClose.setOnMouseExited(e -> {
			btnClose.setStyle(style);
		});

		btnClose.setOnMouseClicked(e -> {
			this.close();
		});
		Button btnHide = new Button("");
		btnHide.setGraphic(new ImageView(new Image("res/image/stage/hide.png")));
		btnHide.setLayoutX(730);
		btnHide.setLayoutY(2);
		btnHide.setStyle(style);
		btnHide.setOnMouseEntered(e -> {
			btnHide.setStyle(styleEntered);
		});

		btnHide.setOnMouseExited(e -> {
			btnHide.setStyle(style);
		});

		btnHide.setOnMouseClicked(e -> {
			this.setIconified(true);
		});

		Button btnHome = new Button("");
		btnHome.setGraphic(new ImageView(new Image("res/image/stage/home.png")));
		btnHome.setLayoutX(700);
		btnHome.setLayoutY(2);
		btnHome.setStyle(style);
		btnHome.setOnMouseEntered(e -> {
			btnHome.setStyle(styleEntered);
		});

		btnHome.setOnMouseExited(e -> {
			btnHome.setStyle(style);
		});

		btnHome.setOnMouseClicked(e -> {
			MultimediaProcessingMain main = new MultimediaProcessingMain();
			try {
				main.start(new Stage());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			this.close();
		});

		Line line = new Line();
		line.setStartX(0);
		line.setStartY(30);
		line.setEndX(800);
		line.setEndY(30);
		line.setFill(Color.WHITE);
		line.setStroke(Color.WHITE);
		line.setStrokeWidth(0.2);

		BorderPane borderPane = new BorderPane();
		borderPane.setPrefSize(800, 467);
		borderPane.setLayoutX(0);
		borderPane.setLayoutY(31);
		buildBorderPane(borderPane);

		pane.getChildren().add(btnClose);
		pane.getChildren().add(btnHide);
		pane.getChildren().add(line);
		pane.getChildren().add(btnHome);
		pane.getChildren().add(borderPane);

		Scene scene = new Scene(pane);

		// 窗口拖动
		this.addEventHandler(MouseEvent.ANY, new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				event.consume();
				if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
					xOffset = event.getSceneX();
					yOffset = event.getSceneY();
				} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
					ChangeVideoType.this.setX(event.getScreenX() - xOffset);
					if (event.getScreenY() - yOffset < 0) {
						ChangeVideoType.this.setY(0);
					} else {
						ChangeVideoType.this.setY(event.getScreenY() - yOffset);
					}
				}
			}
		});
		this.setScene(scene);
		this.initStyle(StageStyle.UNDECORATED);// 设定窗口无边框
		this.getIcons().add(new Image("res/image/stage/logo.png"));
		
	}

	

	private void buildBorderPane(BorderPane borderPane) {
		// TODO Auto-generated method stub
		String btnStyle = "-fx-background-color:#808a87";
		String btnSelectStyle = "-fx-background-color:#87ceeb";
		Pane paneLeft = new Pane();
		paneLeft.setPrefSize(150, 377);
		paneLeft.setStyle("-fx-border-width:0 1 0 0;-fx-border-color:#fff");
		Label lblType = new Label("常见格式：");
		lblType.setFont(Font.font(24));
		lblType.setTextFill(Color.WHITE);
		lblType.setLayoutX(10);
		lblType.setLayoutY(10);

		Button btnMP4 = new Button("转MP4");
		btnMP4.setFont(Font.font(24));
		btnMP4.setPrefSize(120, 50);
		btnMP4.setTextFill(Color.WHITE);
		btnMP4.setStyle(btnSelectStyle);
		btnMP4.setLayoutX(15);
		btnMP4.setLayoutY(44);

		Button btnAVI = new Button("转AVI");
		btnAVI.setFont(Font.font(24));
		btnAVI.setPrefSize(120, 50);
		btnAVI.setTextFill(Color.WHITE);
		btnAVI.setStyle(btnStyle);
		btnAVI.setLayoutX(15);
		btnAVI.setLayoutY(104);

		Button btnMOV = new Button("转MOV");
		btnMOV.setFont(Font.font(24));
		btnMOV.setPrefSize(120, 50);
		btnMOV.setTextFill(Color.WHITE);
		btnMOV.setStyle(btnStyle);
		btnMOV.setLayoutX(15);
		btnMOV.setLayoutY(164);

		Button btnFLV = new Button("转FLV");
		btnFLV.setFont(Font.font(24));
		btnFLV.setPrefSize(120, 50);
		btnFLV.setTextFill(Color.WHITE);
		btnFLV.setStyle(btnStyle);
		btnFLV.setLayoutX(15);
		btnFLV.setLayoutY(224);

		btnMP4.setOnAction(e -> {
			btnMP4.setStyle(btnSelectStyle);
			btnAVI.setStyle(btnStyle);
			btnMOV.setStyle(btnStyle);
			btnFLV.setStyle(btnStyle);
			changeType = ".mp4";
		});

		btnAVI.setOnAction(e -> {
			btnMP4.setStyle(btnStyle);
			btnAVI.setStyle(btnSelectStyle);
			btnMOV.setStyle(btnStyle);
			btnFLV.setStyle(btnStyle);
			changeType = ".avi";
		});

		btnMOV.setOnAction(e -> {
			btnMP4.setStyle(btnStyle);
			btnAVI.setStyle(btnStyle);
			btnMOV.setStyle(btnSelectStyle);
			btnFLV.setStyle(btnStyle);
			changeType = ".mov";
		});

		btnFLV.setOnAction(e -> {
			btnMP4.setStyle(btnStyle);
			btnAVI.setStyle(btnStyle);
			btnMOV.setStyle(btnStyle);
			btnFLV.setStyle(btnSelectStyle);
			changeType = ".flv";
		});
		paneLeft.getChildren().addAll(lblType, btnMP4, btnAVI, btnMOV, btnFLV);

		Pane paneCenter = new Pane();
		paneCenter.setPrefSize(650, 377);
		table.setPrefSize(650, 377);
		table.getStylesheets().add(getClass().getResource("table.css").toExternalForm());
		TableColumn taskCol = new TableColumn("任务");
		taskCol.setMinWidth(200);
		taskCol.setCellValueFactory(new PropertyValueFactory<>("task"));

		TableColumn videoNameCol = new TableColumn("文件名");
		videoNameCol.setMinWidth(300);
		videoNameCol.setCellValueFactory(new PropertyValueFactory<>("videoName"));

		TableColumn oprateCol = new TableColumn("操作");
		oprateCol.setMinWidth(148);
		oprateCol.setCellFactory((col) -> {
			TableCell<Video, String> cell = new TableCell<Video, String>() {
				@Override
				public void updateItem(String item, boolean empty) {
					super.updateItem(item, empty);
					this.setText(null);
					this.setGraphic(null);
					if (!empty) {
						Button delBtn = new Button("移除");
						delBtn.setGraphic(new ImageView(new Image("res/image/changetype/remove.png")));
						delBtn.setStyle("-fx-background-color:#00000000");
						delBtn.setFont(Font.font(16));
						delBtn.setTextFill(Color.RED);
						this.setGraphic(delBtn);
						delBtn.setOnMouseClicked((me) -> {
							Video video = this.getTableView().getItems().get(this.getIndex());
							data.remove(video);
							System.out.println("删除 " + video.getVideoName() + " 的记录");
						});
					}
				}

			};
			return cell;
		});

		table.setItems(data);
		table.getColumns().addAll(taskCol, videoNameCol, oprateCol);
		table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Video>() {
			@Override
			public void changed(ObservableValue<? extends Video> observableValue, Video oldItem, Video newItem) {
				// TODO Auto-generated method stub
				if (newItem != null) {
					selectItem = newItem;
				}
			}
		});

		paneCenter.getChildren().add(table);

		Pane paneBottom = new Pane();
		paneBottom.setPrefSize(800, 90);
		paneBottom.setStyle("-fx-border-width:1 0 0 0;-fx-border-color:#fff");
		Label lblPath = new Label("文件输出位置：");
		lblPath.setTextFill(Color.WHITE);
		lblPath.setFont(Font.font(18));
		lblPath.setLayoutX(10);
		lblPath.setLayoutY(10);

		TextField txtPath = new TextField();
		txtPath.setFont(Font.font(16));
		txtPath.setPrefSize(220, 35);
		txtPath.setLayoutX(10);
		txtPath.setLayoutY(38);
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File com = fsv.getHomeDirectory();
		txtPath.setText(com.getPath());

		Button btnSet = new Button("设置");
		btnSet.setFont(Font.font(16));
		btnSet.setTextFill(Color.WHITE);
		btnSet.setStyle("-fx-background-color:#808a87");
		btnSet.setLayoutX(240);
		btnSet.setLayoutY(38);
		btnSet.setOnAction(e -> {
			DirectoryChooser directoryChooser = new DirectoryChooser();
			File file = directoryChooser.showDialog(this);
			if (file != null) {
				txtPath.setText(file.getPath().toString());
			}

		});

		Button btnView = new Button("浏览");
		btnView.setFont(Font.font(16));
		btnView.setTextFill(Color.WHITE);
		btnView.setStyle("-fx-background-color:#808a87");
		btnView.setLayoutX(300);
		btnView.setLayoutY(38);
		btnView.setOnAction(e -> {
			try {
				Desktop.getDesktop().open(new File(txtPath.getText().toString()));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		});
		String style = "-fx-background-color:#00000000;-fx-border-color:#f96501;-fx-border-width:1px";
		String enterStyle = "-fx-background-color:f96501;-fx-border-color:#f96501;-fx-border-width:1px;";
		Button btnAdd = new Button("添加文件");
		btnAdd.setGraphic(new ImageView(new Image("res/image/changetype/add.png")));
		btnAdd.setTextFill(Color.WHITE);
		btnAdd.setFont(Font.font(20));
		btnAdd.setLayoutX(470);
		btnAdd.setLayoutY(30);
		btnAdd.setStyle(style);

		btnAdd.setOnMouseEntered(e -> {
			btnAdd.setGraphic(new ImageView(new Image("res/image/changetype/add1.png")));
			btnAdd.setTextFill(Color.BLACK);
			btnAdd.setStyle(enterStyle);
		});

		btnAdd.setOnMouseExited(e -> {
			btnAdd.setGraphic(new ImageView(new Image("res/image/changetype/add.png")));
			btnAdd.setTextFill(Color.WHITE);
			btnAdd.setStyle(style);
		});

		Button btnBegin = new Button("开始转换");
		btnBegin.setGraphic(new ImageView(new Image("res/image/changetype/begin.png")));
		btnBegin.setFont(Font.font(20));
		btnBegin.setTextFill(Color.WHITE);
		btnBegin.setStyle("-fx-background-color:#808a87");
		btnBegin.setLayoutX(630);
		btnBegin.setLayoutY(30);
		btnBegin.setDisable(true);

		btnAdd.setOnMouseClicked(e -> {
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle("选择视频");
			fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Video", "*.*"),
					new FileChooser.ExtensionFilter("MP4", "*.mp4"), new FileChooser.ExtensionFilter("AVI", "*.avi"),
					new FileChooser.ExtensionFilter("MOV", "*.mov"), new FileChooser.ExtensionFilter("FLV", "*.flv"));
			File file = fileChooser.showOpenDialog(this);
			if (file != null) {
				if (file.getName().substring(file.getName().indexOf("."), file.getName().length()).equals(changeType)) {
					Alert alert = new Alert(AlertType.INFORMATION);
					alert.setContentText("该文件已是您选中的格式！！！");
					alert.setHeaderText("提示");
					alert.showAndWait();
					return;
				}
				Video video = new Video(file.getName().toString(), "转" + changeType, file.getPath());
				data.add(video);
				btnBegin.setDisable(false);
			}
		});

		btnBegin.setOnMouseClicked(e -> {
			if (selectItem!=null) {
				ConvertVideo convertVideo = new ConvertVideo();
				String fileName = txtPath.getText().toString() + File.separatorChar
						+ selectItem.getVideoName().substring(0, selectItem.getVideoName().indexOf(".")) + changeType;
				convertVideo.convert(new File(selectItem.getPath()), changeType, fileName);
			}else {
				Alert alert=new Alert(AlertType.INFORMATION);
				alert.setContentText("请选择需要转换的视频！！！");
				alert.setTitle("提示");
				alert.showAndWait();
			}
			
		});
		paneBottom.getChildren().addAll(lblPath, txtPath, btnSet, btnView, btnAdd, btnBegin);

		borderPane.setLeft(paneLeft);
		borderPane.setBottom(paneBottom);
		borderPane.setCenter(paneCenter);
	}

}
